package com.encryption.rpc.facade
{
	import com.encryption.rpc.RPCEvent;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class UserFacade
	{
		public static const OPERATION_Login:String = "login";
		public static const OPERATION_Logout:String = "logout";
		public static const OPERATION_AddUser:String = "addUser";
		public static const OPERATION_updateUser:String = "updateUser";
		public static const OPERATION_auditUser:String = "auditUser";
		public static const OPERATION_changePassword:String = "changePassword";
		public static const OPERATION_deleteUser:String = "deleteUser";
		public static const OPERATION_getUserByDept:String = "getUserByDept";
		public static const OPERATION_getUserLoginName:String = "getByLoginName";
		public static const OPERATION_getCSRCmd:String = "getCSRCmd";
		public static const OPERATION_setInstallCmd:String = "setInstallCmd";
		public static const OPERATION_getLogonCmd:String = "getLogonCmd";
		public static const OPERATION_verifyCert:String = "verifyCert";
		public static const OPERATION_getUserByCondition:String = "getUserByCondition";
		public static const OPERATION_sendRandom:String = "sendRandom";
		public static const OPERATION_randomValidate:String = "randomValidate";
		
		private var remoteObject:RemoteObject;
		private var handler:Function;
		private var argsObj:Object;
		
		public function UserFacade(operation:String,argsObj:Object,handler:Function){
			remoteObject = new RemoteObject();
			remoteObject.destination = "userFacade";
			this.handler = handler;
			this.argsObj = argsObj;
			
			if(operation == OPERATION_Login){
				login();
			}else if(operation == OPERATION_Logout){
				logout();
			}else if(operation == OPERATION_AddUser){
				addUser();
			}else if(operation == OPERATION_updateUser){
				updateUser();
			}else if(operation == OPERATION_auditUser){
				auditUser();
			}else if(operation == OPERATION_changePassword){
				changePassword();
			}else if(operation == OPERATION_deleteUser){
				deleteUser();
			}else if(operation == OPERATION_getUserByDept){
				getUserByDept();
			}else if(operation == OPERATION_getUserLoginName){
				getByLoginName();
			}else if(operation == OPERATION_getCSRCmd){
				getCSRCmd();
			}else if(operation == OPERATION_setInstallCmd){
				setInstallCmd();
			}else if(operation == OPERATION_getLogonCmd){
				getLogonCmd();
			}else if(operation == OPERATION_verifyCert){
				verifyCert();
			}else if(operation == OPERATION_getUserByCondition){
				getUserByCondition();
			}else if(operation == OPERATION_sendRandom){
				sendRandom();
			}else if(operation == OPERATION_randomValidate){
				randomValidate();
			}
		}
		
		private function login():void{
			remoteObject.login(argsObj.arg0, argsObj.arg1);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function logout():void{
			remoteObject.logout();
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function addUser():void{
			remoteObject.addUser(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function updateUser():void{
			remoteObject.updateUser(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function auditUser():void{
			remoteObject.auditUser(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function changePassword():void{
			remoteObject.changePassword(argsObj.arg0, argsObj.arg1);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function deleteUser():void{
			remoteObject.deleteUser(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getUserByDept():void{
			remoteObject.getUserByDept(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getByLoginName():void{
			remoteObject.getByLoginName(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getCSRCmd():void{
			remoteObject.getCSRCmd();
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function setInstallCmd():void{
			remoteObject.setInstallCmd(argsObj.arg0, argsObj.arg1);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getLogonCmd():void{
			remoteObject.getLogonCmd(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function verifyCert():void{
			remoteObject.verifyCert(argsObj.arg0, argsObj.arg1);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function getUserByCondition():void{
			remoteObject.getUserByCondition(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function randomValidate():void{
			remoteObject.randomValidate(argsObj.arg0, argsObj.arg1);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function sendRandom():void{
			remoteObject.sendRandom(argsObj.arg0);
			remoteObject.addEventListener(FaultEvent.FAULT,onFault);
			remoteObject.addEventListener(ResultEvent.RESULT,onResult);
		}
		
		private function onFault(evt:FaultEvent):void{
			var rpcEvent:RPCEvent = new RPCEvent(evt);
			this.handler(rpcEvent);
		}
		
		private function onResult(evt:ResultEvent):void{
			remoteObject.disconnect();
			var rpcEvent:RPCEvent = new RPCEvent(evt);
			this.handler(rpcEvent);
		}
	}
}